package view.manager;

import java.awt.EventQueue;
import java.util.ArrayList;
import java.util.Calendar;

import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.ImageIcon;
import javax.swing.JComboBox;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.SwingConstants;

import base.NumberGeneration;
import dao.ClassinformationDao;
import dao.MajorDao;
import model.Classinformation;
import model.Department;
import model.Major;
import util.StringUtil;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class ClassAddInterfrm extends JInternalFrame {
	private JTextField textField;
	private JTextField textField_1;
	private JTextField textField_2;
	private JComboBox comboBox;
	private JComboBox comboBox_1;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ClassAddInterfrm frame = new ClassAddInterfrm();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public ClassAddInterfrm() {
		setClosable(true);
		setIconifiable(true);
		setTitle("班级添加");
		setBounds(100, 100, 401, 300);
		getContentPane().setLayout(null);
		
		JLabel lblNewLabel = new JLabel("所在系：");
		lblNewLabel.setIcon(new ImageIcon(ClassAddInterfrm.class.getResource("/images/describe.png")));
		lblNewLabel.setBounds(75, 28, 79, 15);
		getContentPane().add(lblNewLabel);
		
		comboBox = new JComboBox();
		comboBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (comboBox_1.getSelectedIndex() == 0) {
					textField.setText("");
					return;
				}
				if (comboBox.getSelectedItem() instanceof Major) {
					textField.setText(NumberGeneration.createClassNum((Major) comboBox.getSelectedItem(),(Integer)comboBox_1.getSelectedItem()));
					textField_1.setText(NumberGeneration.createClassName((Major) comboBox.getSelectedItem(),(Integer)comboBox_1.getSelectedItem()));
				} else {
					textField.setText("");
				}
			}
		});
		comboBox.setBounds(179, 24, 127, 23);
		getContentPane().add(comboBox);
		
		JLabel lblNewLabel_1 = new JLabel("班级编号：");
		lblNewLabel_1.setIcon(new ImageIcon(ClassAddInterfrm.class.getResource("/images/num.png")));
		lblNewLabel_1.setBounds(72, 108, 102, 15);
		getContentPane().add(lblNewLabel_1);
		
		textField = new JTextField();
		textField.setEditable(false);
		textField.setText("");
		textField.setBounds(178, 104, 127, 21);
		getContentPane().add(textField);
		textField.setColumns(10);
		
		JLabel lblNewLabel_2 = new JLabel("班级名称：");
		lblNewLabel_2.setIcon(new ImageIcon(ClassAddInterfrm.class.getResource("/images/班级.png")));
		lblNewLabel_2.setBounds(72, 142, 96, 15);
		getContentPane().add(lblNewLabel_2);
		
		textField_1 = new JTextField();
		textField_1.setBounds(178, 139, 129, 21);
		getContentPane().add(textField_1);
		textField_1.setColumns(10);
		
		JLabel lblNewLabel_3 = new JLabel("年级：");
		lblNewLabel_3.setIcon(new ImageIcon(ClassAddInterfrm.class.getResource("/images/年级.png")));
		lblNewLabel_3.setBounds(72, 68, 96, 15);
		getContentPane().add(lblNewLabel_3);
		
		comboBox_1 = new JComboBox();
		comboBox_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (comboBox.getSelectedIndex() == 0) {
					textField.setText("");
					return;
				}
				if (comboBox_1.getSelectedItem() instanceof Integer) {
					textField.setText(NumberGeneration.createClassNum((Major) comboBox.getSelectedItem(),(Integer)comboBox_1.getSelectedItem()));
					textField_1.setText(NumberGeneration.createClassName((Major) comboBox.getSelectedItem(),(Integer)comboBox_1.getSelectedItem()));
				} else {
					textField.setText("");
				}
			}
		});
		comboBox_1.setBounds(178, 67, 127, 23);
		getContentPane().add(comboBox_1);
		
		JLabel lblNewLabel_4 = new JLabel("班级人数：");
		lblNewLabel_4.setIcon(new ImageIcon(ClassAddInterfrm.class.getResource("/images/总人数.png")));
		lblNewLabel_4.setBounds(71, 181, 86, 15);
		getContentPane().add(lblNewLabel_4);
		
		textField_2 = new JTextField();
		textField_2.setBounds(179, 177, 128, 21);
		getContentPane().add(textField_2);
		textField_2.setColumns(10);
		
		JButton btnNewButton = new JButton("添加");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (StringUtil.isEmpty(textField.getText())) {
					JOptionPane.showMessageDialog(null, "请先选择专业和班级", "提示", JOptionPane.ERROR_MESSAGE);
					textField.requestFocus();
					return;
				}
				if (StringUtil.isEmpty(textField_2.getText())) {
					JOptionPane.showMessageDialog(null, "班级人数不能为空", "提示", JOptionPane.ERROR_MESSAGE);
					textField_2.requestFocus();
					return;
				}
				if (!StringUtil.isNum(textField_2.getText())) {
					JOptionPane.showMessageDialog(null, "班级人数需要为数字", "提示", JOptionPane.ERROR_MESSAGE);
					textField_2.requestFocus();
					textField_2.selectAll();
					return;
				}
				if (Integer.parseInt(textField_2.getText())>60 || Integer.parseInt(textField_2.getText())<0 ) {
					JOptionPane.showMessageDialog(null, "班级人数在0~60", "提示", JOptionPane.ERROR_MESSAGE);
					textField_2.requestFocus();
					textField_2.selectAll();
					return;
				}
				Classinformation classinformation = new Classinformation();
				classinformation.setCla_id(textField.getText());
				Major major = (Major)comboBox.getSelectedItem();
				classinformation.setM_id(major.getM_id());
				classinformation.setCla_name(textField_1.getText());
				Integer integer = (Integer)comboBox_1.getSelectedItem();
				classinformation.setCla_grade(integer.toString());
				classinformation.setCla_number(textField_2.getText());
				if (ClassinformationDao.add(classinformation)>0) {
					JOptionPane.showMessageDialog(null, "添加成功", "提示", JOptionPane.INFORMATION_MESSAGE);
					comboBox.setSelectedIndex(0);
					comboBox_1.setSelectedIndex(0);
					textField.setText("");
					textField_1.setText("");
					textField_2.setText("");
				}
			}
		});
		btnNewButton.setIcon(new ImageIcon(ClassAddInterfrm.class.getResource("/images/add.png")));
		btnNewButton.setBounds(93, 229, 93, 23);
		getContentPane().add(btnNewButton);
		
		JButton btnNewButton_1 = new JButton("重置");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				comboBox.setSelectedIndex(0);
				comboBox_1.setSelectedIndex(0);
				textField.setText("");
				textField_1.setText("");
				textField_2.setText("");
			}
		});
		btnNewButton_1.setIcon(new ImageIcon(ClassAddInterfrm.class.getResource("/images/resets.png")));
		btnNewButton_1.setBounds(196, 229, 93, 23);
		getContentPane().add(btnNewButton_1);
		fillMajorType();
		fillGradeType();
	}
	/**
	 * 初始化专业下拉框
	 */
	private void fillMajorType() {
		comboBox.addItem("请选择专业");
		ArrayList<Major> arrayList = MajorDao.select();
		for (Major major : arrayList) {
			comboBox.addItem(major);
		}
	}
	/**
	 * 初始化年级下拉框
	 */
	Integer[] gradelArr = {0,0,0,0};
	int year = NumberGeneration.getDate();
	private void fillGradeType() {
		for (int i = 0; i < 4; i++) {
			gradelArr[i] = year - i;
		}
		comboBox_1.addItem("请选择年级");
		for (int i : gradelArr) {
			comboBox_1.addItem(i);
		}
	}
}
